<?php namespace app\admin\controller;

use houdunwang\request\Request;
use houdunwang\route\Controller;
use system\model\Article as ArticleModel;
use system\model\Category;
use module\Wechat;
use system\model\Keywords;

class Article extends Common {

	use Wechat;

	//中间件实现局部可控制性
	public function __construct()
	{
		$this->auth();
	}

	//动作
    public function index(ArticleModel $article){
    //此处书写代码...
		$field = $article->paginate(2);
		return view('',compact('field'));
    }

    //添加+编辑
	public function post(ArticleModel $article,Category $category)
	{
		$arc_id = Request::get('arc_id',0,'intval');
		//通过get参数的有无来判断是添加还是编辑
		//有get参数是编辑
		//无get参数是添加
		if(IS_POST)
		{
			if($arc_id)
			{
				$article = ArticleModel::find($arc_id);
			}
			//将数据存储在文章表中
			$post = Request::post();
			$article->save($post);
			//添加关键词表数据
			$data =['keywords'=>$post['arc_keyword'],'module_id'=>$arc_id? : $article['arc_id'],'module'=>'article'];
			//保存到数据库中
			$this->saveKeywords($data);
			$this->setRedirect('index')->success('操作成功');
		}
		if($arc_id)
		{
			//编辑
			$oldData = ArticleModel::find($arc_id);
		}
		//获取所属栏目数据
		$cateData = $category->getAllCate();
		return view('',compact('cateData','oldData'));
	}

	//删除
	public function remove(ArticleModel $article)
	{
		$arc_id = Request::get('arc_id');
		//执行删除
		$model = ArticleModel::find($arc_id);
		$keywords = Keywords::where('keywords',$model['arc_keyword'])->first();

		//执行删除
		$keywords->destory();
		$model->destory();
		//跳转
		return $this->setRedirect('index')->success('操作成功');
	}
}
